package com.doit.day01;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;


import java.util.List;

public class DeleteDemo {
    public static void main(String[] args) throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181",2000,null);
//        zooKeeper.delete("/doit/teacher1",-1);
        rmr(zooKeeper,"/doit");

        //关闭资源
        zooKeeper.close();
    }
    public static void rmr(ZooKeeper zooKeeper,String path) throws Exception {
        List<String> children = zooKeeper.getChildren(path, null);
        if(children != null && children.size() >0){
            for (String path1 : children) {
                rmr(zooKeeper,path + "/" +path1);
                zooKeeper.delete(path + "/" +path1,-1);
                System.out.println("当前删除的节点是："+path + "/" +path1);
            }
            zooKeeper.delete(path,-1);
            System.out.println("当前删除的节点是："+path);
        }

    }
    public static void getAllChild(ZooKeeper zooKeeper,String path) throws Exception, KeeperException {
        List<String> children = zooKeeper.getChildren(path, null);
        //能拿到他的子节点  ==》子节点下面是不是有东西
        if(children != null && children.size() >0){
            for (String path1 : children) {
                if (path.equals("/")){
                    getAllChild(zooKeeper,path +path1);
                    System.out.println("节点是："+path +path1);
                }else {
                    getAllChild(zooKeeper,path + "/" +path1);
                    System.out.println("节点是："+path+ "/" + path1);
                }
            }
        }
    }



}
